//
//  WSMyLikeReqest.m
//  CharonTVProject
//
//  Created by karong on 2023/8/14.
//

#import "WSMyLikeReqest.h"

@implementation WSMyLikeReqest
+ (void)loadcharonTV_LikeListWithPage:(NSInteger)page success:(charonTV_ArrayBlock)success failed:(Failure)failed {
    NSString * url = API_CHARONTV_USER_LIKE_LIST;
    NSDictionary * parameters = @{@"page":@(page)};
    [self postWithURLString:url parameters:parameters success:^(NSDictionary * _Nonnull responseDict) {
        NSArray * array = [WSVideo mj_objectArrayWithKeyValuesArray:responseDict[@"items"]];
        success(array);
    } failure:failed];
}

+ (void)delcharonTV_LikeWithChapter_ids:(NSArray *)chapter_ids success:(charonTV_EmptyBlock)success failed:(Failure)failed {
    NSString * url = API_CHARONTV_USER_LIKE_DEL;
    NSMutableString * ids = [NSMutableString new];
    for (WSVideo * video in chapter_ids) {
        if (ids.length > 0) {
            [ids appendString:@","];
        }
        [ids appendString:WSSF(@"%ld", (long)video.chapter_id)];
    }
    NSDictionary * parameters = @{@"chapter_ids":ids,
    };
    [self postWithURLString:url parameters:parameters success:^(NSDictionary * _Nonnull responseDict) {
        success();
    } failure:failed];
}
#pragma mark - PagableRequestPotocol
- (void)refreshDataSuccess:(charonTV_ArrayBlock)success failed:(Failure)failed {
    if (self.loading) {
        return;
    }
    self.loading = YES;
    self.page = 1;
    self.haveMore = YES;
    [WSMyLikeReqest loadcharonTV_LikeListWithPage:self.page success:^(NSArray *array) {
        [self loadSuccess:success withArray:array];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        failed(response, error, responseObject);
        self.loading = NO;
    }];
}


- (void)loadMoreDataSuccess:(charonTV_ArrayBlock)success failed:(Failure)failed {
    if (self.loading && !self.haveMore) {
        return;
    }
    [WSMyLikeReqest loadcharonTV_LikeListWithPage:self.page success:^(NSArray *array) {
        [self loadSuccess:success withArray:array];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        failed(response, error, responseObject);
        self.loading = NO;
    }];
}

- (void)loadSuccess:(charonTV_ArrayBlock)success withArray:(NSArray *)array {
    self.haveMore = array.count == 10;
    self.page++;
    self.loading = NO;
    success(array);
}
@end
